package com.tanhua.dubbo.api.controller;

import com.tanhua.dubbo.api.exception.BusinessException;
import com.tanhua.dubbo.api.interceptor.UserHolder;
import com.tanhua.dubbo.api.service.*;
import com.tanhua.dubbo.api.utils.JwtUtil;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.ExceptionResult;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private UserInfoService userInfoService;

    @PostMapping("/login")
    public void login(@RequestBody Map<String,String> param) throws Exception{
        String phone = param.get("phone");
        userService.sendSms(phone);
    }
    @PostMapping("/loginVerification")
    public ResponseEntity loginVerification(@RequestBody Map<String,String> map){
//        try {
        //1 获取参数
        String phone = map.get("phone");
        String verificationCode = map.get("verificationCode");
        //2 调用UserService完成业务
        Map reMap = userService.loginVerification(phone,verificationCode);
        //3 返回
        return ResponseEntity.ok(reMap);
//        }catch (BusinessException be){
//            ExceptionResult er = be.getExceptionResult();
//            return ResponseEntity.status(500).body(er);
//        }catch (Exception e){
//            return ResponseEntity.status(500).body(ExceptionResult.error());
//        }
    }

    /**
     * 方式一
     */
//    @Autowired
//    private HttpServletRequest request;
//
//    @PostMapping("/loginReginfo1")
//    public ResponseEntity loginReginfo1(){
//        String token = request.getHeader("authorization");
//        return null;
//    }

    /**
     * 方式二
     */
//    @PostMapping("/loginReginfo2")
//    public ResponseEntity loginReginfo2(HttpServletRequest request){
//        String token = request.getHeader("authorization");
//        return null;
//    }

    /**
     * 方式三
     */
    @PostMapping("/loginReginfo")
    public ResponseEntity loginReginfo(@RequestHeader("authorization") String token, @RequestBody UserInfo userInfo){
        /**
         * 1 验证token是否合法
         * 2 获取token中的用户id
         * 3 调用service保存用户扩展信息
         */
//        boolean flag = JwtUtil.verifyToken(token);
//        if (!flag){
//            return ResponseEntity.status(401).body(null);
//        }
//        //验证通过
//        Claims claims = JwtUtil.getClaims(token);
//        Long id = Long.valueOf(claims.get("id").toString());


        //将id给UserInfo。调用UserInfoService保存用户信息
        userInfo.setId(UserHolder.getId());
        if (userInfoService.findById(userInfo.getId())==null)
            userInfoService.save(userInfo);
        return ResponseEntity.ok(null);
    }

    @PostMapping("/loginReginfo/head")
    public ResponseEntity loginReginfoHead(@RequestHeader("authorization") String token, MultipartFile headPhoto) throws Exception{
        //1 解析token，获取id
//        boolean flag = JwtUtil.verifyToken(token);
//        if (!flag){
//            return ResponseEntity.status(401).body(null);
//        }
//        //验证通过
//        Claims claims = JwtUtil.getClaims(token);
//        Long id = Long.valueOf(claims.get("id").toString());

//        Long resut = JwtUtil.parseToken(token);
//        if (resut==-1){
//            return ResponseEntity.status(401).body(null);
//        }

        // 2 调用Service完成业务
        userInfoService.updateHead(UserHolder.getId(),headPhoto);

        return ResponseEntity.ok(null);
    }



}
